<template>
  <basic-container>
    <avue-crud :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      :upload-after="uploadAfterForm"
      :upload-preview="uploadPreview"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad">
      <template slot="menuLeft">
        <!-- <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.yinsheng_param_delete"
                   @click="handleDelete">删 除
        </el-button> -->
      </template>
      <template slot="menuRight">
        <!-- <el-button type="success"
                   size="small"
                   plain
                   icon="el-icon-upload2"
                   v-if="permission.yinsheng_param_import"
                   @click="handleImport">导入
        </el-button> -->
        <!-- <el-button type="warning"
          size="small"
          plain
          icon="el-icon-download"
          v-if="permission.yinsheng_param_export"
          @click="handleExport">导出
        </el-button> -->
      </template>
    </avue-crud>
    <el-dialog title="数据导入"
               append-to-body
               :visible.sync="excelBox"
               width="555px">
      <avue-form :option="excelOption"
                 v-model="excelForm"
                 :upload-after="uploadAfter">
        <template slot="excelTemplate">
          <el-button type="primary"
                     @click="handleTemplate">
            点击下载<i class="el-icon-download el-icon--right"></i>
          </el-button>
        </template>
      </avue-form>
    </el-dialog>
  </basic-container>
</template>

<script>
import {getList, getDetail, add, update, remove} from "@/api/rb-core/yinshengparam/yinsheng_param";
import {mapGetters} from "vuex";
import {
  getToken
} from '@/util/auth';
// import { TimeSelect } from "element-ui";
export default {
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: true,
      excelBox:false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height:'auto',
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        delBtn:false,
        selection: true,
        // searchMenuSpan: 6,
        columnBtn: false,
        labelWidth:130,      //弹出表单的标题的统一宽度
        searchLabelWidth: 130, //搜索框标题宽度
        menuWidth: 150,
        column: [
          {
            label: "租户名称",
            prop: "tenantId",
            type: "select",// dicData: [],
            sortable: true,
            search: true,
            dataType: 'number',
            rules: [
              {
                required: true,
                message: "请选择租户名称",
                trigger: "change",
              },
            ],
            // dicData: [],
            dicUrl: "/api/blade-system/tenant/list?size=-1&current=-1&tenantAuditStatus=2",
            props: {
              label: "tenantName",
              value: "tenantId"
            },
            dicFormatter:(data)=>{
              return data.data.records
            },
            filterable: true,
            width: 200,
            overHidden: true
          },
          {
            label: "通道编号",
            prop: "aisleCode",
            type: "input", 
            sortable: true,
      			search: true,
            rules: [{
            		required: true,
        				message: "请输入通道编号",
        				trigger: "blur"
            }],
            width: 150,
            overHidden: true
  				},
          {
            label: "通道类型",
            prop: "aisleKind",
            type: "select", 
            sortable: true,
      			search: true,
            rules: [{
            		required: true,
        				message: "请输入通道类型",
        				trigger: "blur"
            }],
            dataType: "number",
            dicData: [
              {
                label: "银盛",
                value: "0"
              }
            ],
            width: 150,
            overHidden: true
  				},
          {
            label: "银盛商户公钥",
            prop: "yinshengPublicOwn",
            type: "input", 
            overHidden:true,
            sortable: true,
            rules: [{
            		required: true,
        				message: "请输入银盛公钥",
        				trigger: "blur"
            }],
            addDisplay:false,
            editDisplay:false,
            viewDisplay:false,
            overHidden: true,
            width: 150
  				},
          {
            label: "银盛商户私钥",
            prop: "yinshengPrivateOwn",
            type: "input", 
            overHidden:true,
            sortable: true,
            rules: [{
            		required: true,
        				message: "请输入银盛私钥",
        				trigger: "blur"
            }],
            addDisplay:false,
            editDisplay:false,
            viewDisplay:false,
            overHidden: true,
            width: 150
  				},
          {
            label: '银盛商户公钥',
            prop: 'yinshengPublicOwnArr',
            type: 'upload',
            drag: true,
            loadText: '上传中，请稍等',
            span: 24,
            tip: '请上传 .cer 标准格式文件',
            action: "/api/rb-core/core/yinshengparam/yinsheng_param/uploadLocal",
            // action: "/api/blade-resource/oss/endpoint/put-file-public-plus",
            viewDisplay:true,
            rules: [{
            		required: true,
        				message: "请上传 .cer 标准格式文件",
        				trigger: "blur"
            }],
            limit:1,
            accept:['.cer'],
            propsHttp: {
              url:'link',
              // name:'originalName',
              name:'name',
              res: 'data'
            },
            onRemove:(file,fileList)=>{
              this.form.yinshengPublicOwn="";
              this.form.yinshengPublicOwnArr=fileList;
            },
            hide:true,
            overHidden: true,
          },
          {
            label: '银盛商户私钥',
            prop: 'yinshengPrivateOwnArr',
            type: 'upload',
            drag: true,
            loadText: '上传中，请稍等',
            span: 24,
            tip: '请上传 .sm2 标准格式文件',
            action: "/api/rb-core/core/yinshengparam/yinsheng_param/uploadLocal",
            // action: "/api/blade-resource/oss/endpoint/put-file-public-plus",
            viewDisplay:true,
            rules: [{
            		required: true,
        				message: "请上传 .sm2 标准格式文件",
        				trigger: "blur"
            }],
            limit:1,
            accept:['.sm2'],
            propsHttp: {
              url:'link',
              // name:'originalName',
              name:'name',
              res: 'data'
            },
            onRemove:(file,fileList,index)=>{
              this.form.yinshengPrivateOwn="";
              this.form.yinshengPrivateOwnArr=fileList;
            },
            hide:true,
            overHidden: true,
          },
          {
            label: '银盛公钥',
            prop: 'yinshengPublicArr',
            type: 'upload',
            drag: true,
            loadText: '上传中，请稍等',
            span: 24,
            tip: '请上传 .cer 标准格式文件',
            action: "/api/rb-core/core/yinshengparam/yinsheng_param/uploadLocal",
            // action: "/api/blade-resource/oss/endpoint/put-file-public-plus",
            viewDisplay:true,
            rules: [{
            		required: true,
        				message: "请上传 .cer 标准格式文件",
        				trigger: "blur"
            }],
            limit:1,
            accept:['.cer'],
            propsHttp: {
              url:'link',
              // name:'originalName',
              name:'name',
              res: 'data'
            },
            onRemove:(file,fileList)=>{
              this.form.yinshengPublic="";
              this.form.yinshengPublicArr=fileList;
            },
            hide:true,
            overHidden: true,
          },
          {
            label: "银盛支付密码",
            prop: "yinshengPassword",
            type: "input", 
            sortable: true,
            rules: [{
            		required: true,
        				message: "请输入银盛支付密码",
        				trigger: "blur"
            }],
  				  // viewDisplay:false,
            width: 150,
            overHidden: true
          },
          {
            label: "银盛业务编码",
            prop: "yinshengBusinessCode",
            type: "input", 
            sortable: true,
            rules: [{
            		required: true,
        				message: "请输入银盛业务编码",
        				trigger: "blur"
            }],
            width: 150,
            overHidden: true
  				},
          {
            label: "银盛商户号",
            prop: "yinshengMerchantCode",
            type: "input", 
            sortable: true,
      			search: true,
            rules: [{
            		required: true,
        				message: "请输入银盛商户号",
        				trigger: "blur"
            }],
            width: 150,
            overHidden: true
  				},
          {
            label: "支付宝应用私钥",
            prop: "aliPrivateOwn",
            type: "textarea", 
            sortable: true,
            rules: [{
            		required: true,
        				message: "请输入支付宝应用私钥",
        				trigger: "blur"
            }],
            overHidden:true,
            span: 24,
            overHidden: true
  				},
          {
            label: "支付宝应用公钥",
            prop: "aliPublicOwn",
            type: "textarea", 
            sortable: true,
            rules: [{
            		required: true,
        				message: "请输入支付宝应用公钥",
        				trigger: "blur"
            }],
            overHidden:true,
            span: 24,
            overHidden: true
  				},
          {
            label: "支付宝公钥",
            prop: "aliPublic",
            type: "textarea", 
            sortable: true,
            rules: [{
            		required: true,
        				message: "请输入支付宝公钥",
        				trigger: "blur"
            }],
            overHidden:true,
            span: 24,
            overHidden: true
  				},
          // {
          //   label: "状态",
          //   prop: "status",
          //   type: "input", 
          //   sortable: true,
  				// },
          {
            label: "通道机构号",
            prop: "cerId",
            type: "input", 
            hide:true,
            rules: [{
            		required: true,
        				message: "请输入通道机构号",
        				trigger: "blur"
            }],
            overHidden: true
  				},
          {
            label: "微信appid",
            prop: "wxAppId",
            type: "input", 
            hide:true,
            rules: [{
            		required: true,
        				message: "请输入微信appid",
        				trigger: "blur"
            }],
            overHidden: true
  				},          
          {
            label: "银联用户端ip",
            prop: "spbillCreateIp",
            type: "input", 
            hide:true,
            rules: [{
            		required: true,
        				message: "请输入银联用户端ip",
        				trigger: "blur"
            }],
            overHidden: true
  				},
          {
            label: "阿里appid",
            prop: "aliAppId",
            type: "input", 
            hide:true,
            rules: [{
            		required: true,
        				message: "请输入阿里appid",
        				trigger: "blur"
            }],
            overHidden: true
  				},
          {
            label: "支付回调地址",
            prop: "payNotifyUrl",
            type: "textarea", 
            hide:true,
            rules: [{
            		required: true,
        				message: "请输入支付回调地址",
        				trigger: "blur"
            }],
            overHidden: true
  				},
          {
            label: "退款回调地址",
            prop: "refundNotifyUrl",
            type: "textarea", 
            hide:true,
            rules: [{
            		required: true,
        				message: "请输入退款回调地址",
        				trigger: "blur"
            }],
            overHidden: true
  				},
        ]
  },
    data: [],
      excelForm: {},
    excelOption: {
      submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: '模板上传',
            prop: 'excelFile',
            type: 'upload',
            drag: true,
            loadText: '模板上传中，请稍等',
            span: 24,
            propsHttp: {
              res: 'data'
            },
            tip: '请上传 .xls,.xlsx 标准格式文件',
            action: "/api/rb-core/core/yinshengparam/yinsheng_param/import"
          },
            {
              label: '模板下载',
              prop: 'excelTemplate',
              formslot: true,
              span: 24,
            }
          ]
    }
  };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.yinsheng_param_add, false),
        viewBtn: this.vaildData(this.permission.yinsheng_param_view, false),
        delBtn: this.vaildData(this.permission.yinsheng_param_delete, false),
        editBtn: this.vaildData(this.permission.yinsheng_param_edit, false)
    };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    uploadPreview(file,column,done){
      // 此处不需要查看
      window.location.href = file.url;
      // done()//默认执行打开方法
    },
    uploadAfterForm(res, done) {
      done();
    },
    rowSave(row, done, loading) {
      row.yinshengPrivateOwn=row.yinshengPrivateOwnArr[0].value;
      row.yinshengPublicOwn=row.yinshengPublicOwnArr[0].value;
      row.yinshengPublic=row.yinshengPublicArr[0].value;

      add(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        window.console.log(error);
      });
    },
    rowUpdate(row, index, done, loading) {
      row.yinshengPrivateOwn=row.yinshengPrivateOwnArr[0].value;
      row.yinshengPublicOwn=row.yinshengPublicOwnArr[0].value;
      row.yinshengPublic=row.yinshengPublicArr[0].value;

      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        window.console.log(error);
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
          if(this.form.yinshengPublicOwn){
            const DATA =this.form.yinshengPublicOwn.split("\\")
            this.form.yinshengPublicOwnArr=[{
              value:this.form.yinshengPublicOwn,
              label:DATA[DATA.length-1]
            }]
          }
          if(this.form.yinshengPrivateOwn){
            const DATA =this.form.yinshengPrivateOwn.split("\\")
            this.form.yinshengPrivateOwnArr=[{
              value:this.form.yinshengPrivateOwn,
              label:DATA[DATA.length-1]
            }]
          }
          if(this.form.yinshengPublic){
            const DATA =this.form.yinshengPublic.split("\\")
            this.form.yinshengPublicArr=[{
              value:this.form.yinshengPublic,
              label:DATA[DATA.length-1]
            }]
          }
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage){
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize){
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }, 
    handleImport() {
      this.excelBox = true;
    },
    handleExport() {
      let exQuery = ``;
      exQuery += `&aisleCode=${this.search.aisleCode}`;
      exQuery += `&aisleKind=${this.search.aisleKind}`;
      exQuery += `&yinshengMerchantCode=${this.search.yinshengMerchantCode}`;
      this.$confirm("是否导出用户数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        window.open(
          `/api/rb-core/core/yinshengparam/yinsheng_param/export?${this.website.tokenHeader}=${getToken()}`+exQuery
        );
      });
    },
    handleTemplate() {
      window.open(`/api/rb-core/core/yinshengparam/yinsheng_param/export-template?${this.website.tokenHeader}=${getToken()}`);
    },
    uploadAfter(res, done) {
      this.excelBox = false;
      this.refreshChange();
      done();
    },
      sortChange(val) {
        if (val.order == "ascending") {
          //正序
          this.query.ascs = val.prop
          this.query.descs = ""
        } else {
          //倒序
          this.query.descs = val.prop
          this.query.ascs = ""
        }
        this.refreshChange();
      }
  }
};
</script>

<style>
</style>
